package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 查询产品规格参数
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);

        return this.specGroupMapper.select(specGroup);
    }

    /**
     * 新增产品分类分组
     * @param specGroup
     * @return
     */
    @Transactional
    public int insertGroup(SpecGroup specGroup) {

        return this.specGroupMapper.insertSelective(specGroup);
    }

    /**
     * 修改产品分组
     * @param specGroup
     * @return
     */
    public int updateGroup(SpecGroup specGroup) {

        return this.specGroupMapper.updateByPrimaryKeySelective(specGroup);
    }

    /**
     * 删除产品分组
     * @param id
     * @return
     */
    public int deleteGroup(Integer id) {

        return this.specGroupMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据id查询产品规格参数
     * @param gid
     * @return
     */
    public List<SpecParam> queryGroupsByGid(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setGeneric(generic);
        specParam.setSearching(searching);

        return this.specParamMapper.select(specParam);
    }

    /**
     * 新增产品规格
     * @param specParam
     * @return
     */
    public int insertParams(SpecParam specParam) {

        return this.specParamMapper.insertSelective(specParam);
    }

    /**
     * 修改产品规格参数
     * @param specParam
     * @return
     */
    public int updateParams(SpecParam specParam) {

        return this.specParamMapper.updateByPrimaryKeySelective(specParam);
    }

    /**
     * 删除产品规格参数
     * @param id
     * @return
     */
    public int deleteParams(Integer id) {

        return this.specParamMapper.deleteByPrimaryKey(id);
    }


    public List<SpecGroup> querySpecsByCid(Long cid) {

        // 查询规格组
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(g -> {
            // 查询组内参数
            g.setParams(this.queryGroupsByGid(g.getId(), null, null, null));
        });
        return groups;
    }

    public List<SpecGroup> queryGroupsWithParams(Long cid) {

        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(group->{
            List<SpecParam> params = this.queryParams(group.getId(), null, null, null);
            group.setParams(params);
        });

        return groups;
    }

    /**
     * 根据条件查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.specParamMapper.select(record);
    }
}
